﻿using CentralVacuum.Communication;
using CentralVacuum.Keyboard;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CentralVacuum.Common
{
    class ControlAttribute
    {
        public static void Button_Control(UIButton button, Color color)
        {
            button.FillColor = color;
            button.FillHoverColor = color;
            button.FillPressColor = color;
            button.FillSelectedColor = color;
        }

        public static string TextBoxRead(UITextBox uiTextBox, string data)
        {
            if (uiTextBox.Type.ToString() == "Double")
            {
                if (data.Contains(".") == false)//没有小数点
                {
                    if (uiTextBox.DecimalPlaces == 0)
                    {
                        //string[] strArray = data.Split('.');
                        //data = strArray[0];
                    }
                    else
                    {
                        data += ".";
                        data += new string('0', uiTextBox.DecimalPlaces);
                    }
                }
                else
                {
                    if (data.EndsWith("."))
                    {
                        data += new string('0', uiTextBox.DecimalPlaces);
                    }
                    else
                    {
                        string[] strArray = data.Split('.');
                        if (strArray[1].Length < uiTextBox.DecimalPlaces)
                        {
                            data += new string('0', uiTextBox.DecimalPlaces - strArray[1].Length);
                        }
                        else if (strArray[1].Length > uiTextBox.DecimalPlaces)
                        {
                            data = strArray[0] + "." + strArray[1].Substring(0, uiTextBox.DecimalPlaces);
                        }
                    }
                }
            }
            return data;
        }

        public static int TextBoxWriteInt16(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker)
        {
            short v;
            v = Convert.ToInt16(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToInt16(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteInt16(address, Convert.ToInt16(uiTextBox.Text));
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int TextBoxWriteUInt16(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker)
        {
            ushort v;
            v = Convert.ToUInt16(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToUInt16(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteUInt16(address, Convert.ToUInt16(uiTextBox.Text));
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int TextBoxWriteInt32(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker)
        {
            int v;
            v = Convert.ToInt32(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToInt32(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteInt32(address, Convert.ToInt32(uiTextBox.Text));
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int TextBoxWriteSingleToUInt32(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker, float offset)
        {
            float v;
            v = Convert.ToSingle(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToSingle(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteUInt32(address, Convert.ToUInt32(Convert.ToSingle(uiTextBox.Text) * offset));
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int TextBoxWriteUInt32(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker)
        {
            uint v;
            v = Convert.ToUInt32(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToUInt32(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteUInt32(address, Convert.ToUInt32(uiTextBox.Text));
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int TextBoxWriteSingle(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker)
        {
            float v;
            v = Convert.ToSingle(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToSingle(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteSingle(address, Convert.ToSingle(uiTextBox.Text));
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int TextBoxWriteSingle(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker, float offset)
        {
            float v;
            v = Convert.ToSingle(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToSingle(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteSingle(address, Convert.ToSingle(uiTextBox.Text) * offset);
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int TextBoxWriteInt64(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker)
        {
            long v;
            v = Convert.ToInt64(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToInt64(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteInt64(address, Convert.ToInt64(uiTextBox.Text));
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int TextBoxWriteUInt64(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker)
        {
            ulong v;
            v = Convert.ToUInt64(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToUInt64(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteUInt64(address, Convert.ToUInt64(uiTextBox.Text));
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int TextBoxWriteDouble(UITextBox uiTextBox, string address, BackgroundWorker backgroundWorker)
        {
            double v;
            v = Convert.ToDouble(uiTextBox.Text);
            KeyboardClass.OpenNumberBoard(uiTextBox);
            if (v != Convert.ToDouble(uiTextBox.Text))
            {
                HslMelsecMCBinary.WriteDouble(address, Convert.ToDouble(uiTextBox.Text));
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }

        public static int ComboBoxWriteUInt16(UIComboBox uiComboBox, string address, BackgroundWorker backgroundWorker)
        {
            HslMelsecMCBinary.WriteUInt16(address, Convert.ToUInt16(uiComboBox.SelectedIndex));
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();//后台单线程执行
            return 1;
        }
    }
}
